package com.jjwww.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jjwww.common.QueryPageParam;
import com.jjwww.common.Result;
import com.jjwww.entity.Type;
import com.jjwww.service.impl.TypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjwww
 * @since 2025-10-23
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeServiceImpl typeService;
    //按钮添加
    @PostMapping("/save")
    public Result save(@RequestBody Type type){
        return typeService.save(type)?Result.suc():Result.fail();
    }
    //按钮修改
    @PostMapping("/update")
    public Result update(@RequestBody Type type){
        return typeService.updateById(type)?Result.suc():Result.fail();
    }
    //删除按钮
    @GetMapping("/del")
    public Result del(Integer id){
        return typeService.removeById(id)?Result.suc():Result.fail();
    }
    //查询
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){

        HashMap param = query.getParam();
        String name= (String)param.get("name");

        //总记录数，当前页
        Page<Type> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Type> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Type::getName,name);
        }

        IPage result = typeService.pageBB(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }
    //加载类目参数
    @GetMapping("/list")
    public Result list(){
        List list = typeService.list();
        return Result.suc(list);
    }











    @GetMapping("/list1")
    public List<Type> list1(){
        return typeService.list();
    }
    //新增
    @PostMapping ("/save1")
    public boolean save1(@RequestBody Type type){
        return typeService.save(type);
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Type type){
        return typeService.updateById(type);
    }
    //新增或修改
    @PostMapping("/saveOrmod")
    public boolean saveOrmod(@RequestBody Type type){
        return typeService.saveOrUpdate(type);
    }
    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id){
        return typeService.removeById(id);
    }
    //模糊查询
    @PostMapping("listP")
    public List<Type> list(@RequestBody Type type){
        LambdaQueryWrapper<Type> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Type::getName,type.getName());
        return typeService.list(lambdaQueryWrapper);
    }

}
